package com.example.zwx.myapplication.utils;

import android.text.TextUtils;
import android.util.Log;

import java.util.List;

/**
 * Created by Administrator on 2016/2/26.
 */
public class IrParser {
    private static final String TAG = IrParser.class.getSimpleName();
    public static final int FREQ = 37840;

    //有些红外码是翻转码, 以"&"分割  启动一次应该发送 第一段红外码, 第二次发送第二段
    private static boolean reverse = false;

    /**
     * 获取ble发送时所需红外数据
     *
     * @param pattern  红外字符串
     * @param freq     频率
     * @param interval 时间间隔
     * @return 红外数据
     */
    public static byte[][] parseRawIrData(String pattern, int freq, int interval) {
        if (freq == 0) {
            freq = FREQ;
        }
        int patterns[] = parseIRData(pattern);
        NanoIrPackager nanoIrPackager = new NanoIrPackager();
        return nanoIrPackager.packageWaveCode(freq, patterns, interval);
    }

    /**
     * 获取ble发送时所需红外数据
     *
     * @param pattern  红外int数组
     * @param freq     频率
     * @param interval 时间间隔
     * @return 红外数据
     */
    public static byte[][] parseRawIrData(int[] pattern, int freq, int interval) {
        return new NanoIrPackager().packageWaveCode(freq, pattern, interval);
    }


    /**
     * 根据字符串解析红外int数组
     *
     * @param pattern 红外字符串
     * @return 红外int数组
     */
    public static int[] parseIRData(String pattern) {
        String pulse = null;
        //处理翻转码
        if (pattern != null && pattern.contains("&")) {
            String[] arrayp = pattern.split("&");
            if (arrayp.length > 1) {
                if (reverse) {
                    pulse = arrayp[1];
                    reverse = false;
                } else {
                    pulse = arrayp[0];
                    reverse = true;
                }
            }

        } else {
            pulse = pattern;
        }

        int[] patterns = parseIntArray(pulse, ",");
        return patterns;
    }

    /**
     * 旧版本获取ble发送时所需红外码
     *
     * @param pattern
     * @param freq
     * @return
     */
    public static byte[] parseRawIrData(String pattern, int freq) {
        int[] patterns = parseIRData(pattern);
        return optByteIrData(patterns, freq);
    }

    /**
     * 解析 字符串
     *
     * @param text str
     * @return patterns
     */
    public static int[] parseIntArray(String text, String delimiter) {
        if (TextUtils.isEmpty(text)) {
            return null;
        }
        String[] strings = text.split(delimiter);
        int[] patterns = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            patterns[i] = parseInt(strings[i]);
        }

        return patterns;
    }

    /**
     * change string to integer safely.
     *
     * @param strInt
     * @return
     */
    public static int parseInt(String strInt) {
        int num = 0;
        try {
            num = Integer.parseInt(strInt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 旧版本获取ble要发送的红外码
     *
     * @param pattern   红外数据
     * @param frequency 频率
     * @return 返回ble发送的红外码数组
     */
    public static byte[] optByteIrData(int[] pattern, int frequency) {
        if (frequency == 0) {
            frequency = FREQ;
        }
        int pulseFactor = 1000000 / frequency;
        int[] result = new int[pattern.length * 2];
        for (int i = 0; i < pattern.length; i++) {

            if (i % 2 == 0) {
                result[2 * i] = ((pattern[i] / pulseFactor) >> 8) + 0x80;
                result[2 * i + 1] = ((pattern[i] / pulseFactor)) & 0xFF;
            } else {
                result[2 * i] = ((pattern[i] / pulseFactor)) >> 8;
                result[2 * i + 1] = ((pattern[i] / pulseFactor)) & 0xFF;
            }

        }

        int total = result.length + 8;
        int lineCount = total / 16;
        if (total % 16 > 0) {
            lineCount += 1;
        }
        Log.e(TAG, lineCount + ":  lineCount");

        int[] irData = new int[lineCount * 16];
        // head
        irData[0] = 0x78;
        irData[1] = 0x56;
        irData[2] = 0x34;
        irData[3] = 0x12;
        // frequency
        // irData[4] = pulseFactor;
        irData[4] = 0x26;
        irData[5] = 0x00;

        System.arraycopy(result, 0, irData, 6, result.length);
        // tail
        for (int i = (result.length + 6) - 1; i < irData.length; i++) {
            irData[i] = 0x00;
        }

        byte[] resultData = new byte[irData.length];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < irData.length; i++) {
            resultData[i] = (byte) irData[i];
            sb.append(irData[i]);
            sb.append(",");
        }

        Log.i(TAG, sb.toString() + "irdata : length" + irData.length);
        return resultData;

    }


    public static String getByteString(byte[] buf, int len) {
        String tmp = "";
        if (len < buf.length) {
            len = buf.length;
        }

        for (int i = 0; i < len; ++i) {
            tmp = tmp + String.format("%02x,", new Object[]{Byte.valueOf(buf[i])});
        }

        return tmp;
    }


    public static byte[][] getCombinedIrData(List<String> keys) {
        NanoIrPackager nanoIrPackager = new NanoIrPackager();
        if (keys.size() > 1) {
            int[][] pattern = new int[keys.size()][];
            for (int i = 0; i < keys.size(); i++) {
                String irDataStr = keys.get(i);
                int[] p = parseIRData(irDataStr);
                pattern[i] = p;
            }
            return nanoIrPackager.packageWaveCode(FREQ, pattern, 100000);
        } else {
            return parseRawIrData(keys.get(0), FREQ, 0);
        }

    }

}
